package com.weikuang.fareast.adapter;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.ChangeBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 审核修改
 */
public class ChangeCheckAdapter extends BaseRecyclerAdapter<ChangeBean> {

    private OnRecyclerViewItemClickListener mOnItemClickListener;
    public String exceptionInfo = "";//备注异常信息
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_change_check;
    }

    public ChangeCheckAdapter() {

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        ChangeBean bean = getItem(position);

        holder.getText(R.id.title).setText(bean.title);
        holder.getText(R.id.ed_num).setText(bean.num+"");

//        EditText mExceptionEditText = holder.getView(R.id.ed_num);
//
//        if (mExceptionEditText.getTag(R.id.ed_num) instanceof TextWatcher){

//            mExceptionEditText.removeTextChangedListener(((TextWatcher)mExceptionEditText.getTag(R.id.ed_num)));
//        }
//        //移除了TextWatcher事件后设置item对应的文本
//        if (bean.num == 0) {
//            mExceptionEditText.setText( "");
//        }else {
//            mExceptionEditText.setText(bean.num + "");
//        }
//        mExceptionEditText.setSelection(mExceptionEditText.getText().length());
//        TextWatcher textWatcher=new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.toString().length() > 0) {
//                    bean.num = Integer.valueOf(s.toString());
//                }
//            }
//        };
//        //设置tag为TextWatcher
//        mExceptionEditText.addTextChangedListener(textWatcher);
//        mExceptionEditText.setTag(R.id.ed_num,textWatcher);


        EditText mExceptionEditText_check = holder.getView(R.id.ed_num_check);

        if (mExceptionEditText_check.getTag(R.id.ed_num_check) instanceof TextWatcher){
            mExceptionEditText_check.removeTextChangedListener(((TextWatcher)mExceptionEditText_check.getTag(R.id.ed_num_check)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.num_check == 0) {
            mExceptionEditText_check.setText( "");
        }else {
            mExceptionEditText_check.setText(bean.num_check + "");
        }
        mExceptionEditText_check.setSelection(mExceptionEditText_check.getText().length());
        TextWatcher textWatcher_check=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.num_check = Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_check.addTextChangedListener(textWatcher_check);
        mExceptionEditText_check.setTag(R.id.ed_num_check,textWatcher_check);


        EditText mExceptionEditText_loss = holder.getView(R.id.ed_num_loss);

        if (mExceptionEditText_loss.getTag(R.id.ed_num_loss) instanceof TextWatcher){
            mExceptionEditText_loss.removeTextChangedListener(((TextWatcher)mExceptionEditText_loss.getTag(R.id.ed_num_loss)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.num_loss == 0) {
            mExceptionEditText_loss.setText("");
        }else {
            mExceptionEditText_loss.setText(bean.num_loss + "");
        }
        mExceptionEditText_loss.setSelection(mExceptionEditText_loss.getText().length());
        TextWatcher textWatcher_loss=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.num_loss= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_loss.addTextChangedListener(textWatcher_loss);
        mExceptionEditText_loss.setTag(R.id.ed_num_loss,textWatcher_loss);



        EditText mExceptionEditText_backs = holder.getView(R.id.ed_num_backs);

        if (mExceptionEditText_backs.getTag(R.id.ed_num_backs) instanceof TextWatcher){
            mExceptionEditText_backs.removeTextChangedListener(((TextWatcher)mExceptionEditText_backs.getTag(R.id.ed_num_backs)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.backs == 0) {
            mExceptionEditText_backs.setText("");
        }else {
            mExceptionEditText_backs.setText(bean.backs + "");
        }
        mExceptionEditText_backs.setSelection(mExceptionEditText_backs.getText().length());
        TextWatcher textWatcher_backs=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.backs= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_backs.addTextChangedListener(textWatcher_backs);
        mExceptionEditText_backs.setTag(R.id.ed_num_backs,textWatcher_backs);

        Button btn_sure = holder.getView(R.id.btn_sure);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(view,bean,position);
                }
            }
        });

    }


    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, ChangeBean data, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
